﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Commands;
using UIWindows.Enums;
using UIWindows.GlobalStatics;
using UIWindows.Models;
using UIWindows.Tools.Helper;
using XingLucifer.IBase.Enums;

namespace UIWindows.ViewModels
{
    public class RoleManagementViewModel : XingLucifer.IBase.IViewStatus
    {
        public ObservableCollection<RoleInitModel> RunParameter { get; set; }
        public ObservableCollection<RoleInitModel> MesUrl { get; set; }
        public ObservableCollection<RoleInitModel> CollectionItems { get; set; }
        public ObservableCollection<RoleInitModel> Range { get; set; }
        public ObservableCollection<RoleInitModel> FunctionEnable { get; set; }
        public ObservableCollection<RoleInitModel> Menus { get; set; }
        private ObservableCollection<RoleInitModel> _copyRoleInit;

        private UserConfigStatic _userConfigStatic;
        private DisplayItemsStatic _displayItemsStatic;
        public RoleManagementViewModel(UserConfigStatic userConfigStatic, DisplayItemsStatic displayItemsStatic)
        {
            _userConfigStatic = userConfigStatic;
            _displayItemsStatic = displayItemsStatic;
            RunParameter = new ObservableCollection<RoleInitModel>();
            CollectionItems = new ObservableCollection<RoleInitModel>();
            MesUrl = new ObservableCollection<RoleInitModel>();
            Range = new ObservableCollection<RoleInitModel>();
            FunctionEnable = new ObservableCollection<RoleInitModel>();
            Menus = new ObservableCollection<RoleInitModel>();
            _copyRoleInit = new ObservableCollection<RoleInitModel>();
            _copyAction = ConversionHelper.CopyClass<RoleInitModel, RoleInitModel>();
        }

        public DelegateCommand<string> SelectPermissions => new DelegateCommand<string>(sender =>
        {
            if (sender == null)
            {
                return;
            }
            CollectionItems.Clear();
            MesUrl.Clear();
            FunctionEnable.Clear();
            Range.Clear();
            Menus.Clear();
            RunParameter.Clear();
            AuthorityTypes authority = (AuthorityTypes)Enum.Parse(typeof(AuthorityTypes), sender);
            UpdateData(authority);
        });

        private void UpdateData(AuthorityTypes authority)
        {
            foreach (var item in _displayItemsStatic.RoleInit.Where(x => x.Category == "采集项参数" && x.Authority == authority))
            {
                CollectionItems.Add(item);
            }
            foreach (var item in _displayItemsStatic.RoleInit.Where(x => x.Category == "功能启用" && x.Authority == authority))
            {
                FunctionEnable.Add(item);
            }
            foreach (var item in _displayItemsStatic.RoleInit.Where(x => x.Category == "MES" && x.Authority == authority))
            {
                MesUrl.Add(item);
            }
            foreach (var item in _displayItemsStatic.RoleInit.Where(x => x.Category == "范围" && x.Authority == authority))
            {
                Range.Add(item);
            }
            foreach (var item in _displayItemsStatic.RoleInit.Where(x => x.Category == "菜单" && x.Authority == authority))
            {
                Menus.Add(item);
            }
            foreach (var item in _displayItemsStatic.RoleInit.Where(x => x.Category == "运行参数" && x.Authority == authority))
            {
                RunParameter.Add(item);
            }
        }

        public DelegateCommand<bool?> CollectionItem_SelectALL => new DelegateCommand<bool?>(sender =>
        {
            foreach (var item in CollectionItems)
            {
                item.IsEnable = (bool)sender;
            }
        });

        public DelegateCommand<bool?> FunctionEnable_SelectALL => new DelegateCommand<bool?>(sender =>
        {
            foreach (var item in FunctionEnable)
            {
                item.IsEnable = (bool)sender;
            }
        });

        public DelegateCommand<bool?> MesUrl_SelectALL => new DelegateCommand<bool?>(sender =>
        {
            foreach (var item in MesUrl)
            {
                item.IsEnable = (bool)sender;
            }
        });

        public DelegateCommand<bool?> Range_SelectALL => new DelegateCommand<bool?>(sender =>
        {
            foreach (var item in Range)
            {
                item.IsEnable = (bool)sender;
            }
        });

        public DelegateCommand<bool?> Menus_SelectALL => new DelegateCommand<bool?>(sender =>
        {
            foreach (var item in Menus)
            {
                item.IsEnable = (bool)sender;
            }
        });

        public DelegateCommand<bool?> Run_SelectALL => new DelegateCommand<bool?>(sender =>
        {
            foreach (var item in RunParameter)
            {
                item.IsEnable = (bool)sender;
            }
        });

        public DelegateCommand Save => new DelegateCommand(() =>
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (var item in _copyRoleInit)
            {
                var model = _displayItemsStatic.RoleInit.FirstOrDefault(x => x.RoleName == item.RoleName && item.Authority == x.Authority);
                StringBuilder builder = ContrastObject(item, model, model.RoleName, model.Authority);
                if (builder != null)
                {
                    _copyAction.Invoke(model, item);
                    stringBuilder.Append(builder.ToString());
                }
            }
            if (string.IsNullOrEmpty(stringBuilder.ToString()))
            {
                return;
            }
            $"用户：[{_userConfigStatic.LoginUser.Name}]\r\n{stringBuilder}".MessageDialogs(MessageLevelType.警告, true, Enums.PopUpTypes.Global);
            _displayItemsStatic.SaveRoleInit();
        }, AuthorityTypes.管理员_参照 | AuthorityTypes.超级管理员_参照);

        private Action<RoleInitModel, RoleInitModel> _copyAction;

        public void Load()
        {
            if (CollectionItems.Count == 0)
            {
                UpdateData(AuthorityTypes.操作员);
            }
            foreach (var item in _displayItemsStatic.RoleInit)
            {
                RoleInitModel roleInitModel = new RoleInitModel();
                _copyAction.Invoke(item, roleInitModel);
                _copyRoleInit.Add(roleInitModel);
            }
        }

        public void UnLoad()
        {
            if (_copyRoleInit != null)
            {
                _copyRoleInit.Clear();
            }
        }

        private StringBuilder ContrastObject<T>(T t, T obj, string name, AuthorityTypes authority)
        {
            StringBuilder stringBuilder = new StringBuilder();
            var objProper = obj.GetType().GetProperties();
            foreach (var item in t.GetType().GetProperties())
            {
                PropertyInfo op = objProper.Where(x => x.Name == item.Name).FirstOrDefault();
                if (op != null)
                {
                    if ((item.GetValue(t) as bool?) != (op.GetValue(obj) as bool?))
                    {
                        stringBuilder.AppendLine($"[权限：{authority}==项目：{name}==原值：{item.GetValue(t)}-->更改后：{op.GetValue(obj)}]");
                    }
                }
            }
            return stringBuilder;
        }
    }
}
